package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据条件查询套餐数据
     *
     * @param setmeal
     * @return
     */

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);

    }

    //分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        //创建分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        //创建条件构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.like(Strings.isNotEmpty(name), Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        //执行分页查询
        setmealService.page(pageInfo, lqw);

        //将结果拷贝
        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");
        //创建集合保存SetmealDto对象
        List<SetmealDto> list = new ArrayList<>();
        for (Setmeal record : pageInfo.getRecords()) {
            //将菜品对象信息拷入SetmealDto对象
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            //创建分页构造器
            LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //添加条件,根据添加菜品所属的categoryId
            categoryLambdaQueryWrapper.eq(Category::getId, record.getCategoryId());
            //执行查询,查询出套餐所对应的分类
            Category one = categoryService.getOne(categoryLambdaQueryWrapper);
            if (one != null) {
                //获取套餐的分类名称,将其赋给SetmealDto的categoryName属性
                setmealDto.setCategoryName(one.getName());
                //将单个SetmealDto对象加入SetmealDtos集合
                list.add(setmealDto);
            }
        }
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    //新增套餐
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    //删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    //通过setmealId获取套餐信息
    @GetMapping("/{setmealId}")
    public R<SetmealDto> getOne(@PathVariable Long setmealId){
        SetmealDto setmealDto = setmealService.getWithDish(setmealId);
        return R.success(setmealDto);
    }

    //修改
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("套餐修改成功");
    }

    //修改状态
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        log.info("进入了对状态进行修改的部分");
        //创建条件生成器
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        //获取所有需要修改状态的套餐
        List<Setmeal> list = setmealService.list(lqw);
        //更新套餐的状态
        list.forEach(setmeal -> setmeal.setStatus(status));
        setmealService.updateBatchById(list);
        return R.success("修改成功");
    }
}
